//
//  SuperViewController.m
//  kuihuajingyingwang
//
//  Created by smm on 16/8/24.
//  Copyright © 2016年 com.huishangbao. All rights reserved.
//

#import "SuperViewController.h"
#import "IQKeyboardManager.h"
#import "IQKeyboardReturnKeyHandler.h"


@interface SuperViewController ()<UIGestureRecognizerDelegate,UIAlertViewDelegate>

@property (nonatomic,strong) UIRefreshControl * pullDownRefresh;
@property (nonatomic, strong) IQKeyboardReturnKeyHandler    *returnKeyHandler;
@end

@implementation SuperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _SCREENH = ([[UIScreen mainScreen] bounds].size.height);
    _SCREENW = ([[UIScreen mainScreen] bounds].size.width);
    self.returnKeyHandler=
    [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
    self.returnKeyHandler.lastTextFieldReturnKeyType = UIReturnKeyNext;
    NSString * name = GDLocalizedString(@"done");
    [[IQKeyboardManager sharedManager] setToolbarDoneBarButtonItemText:name];
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [IQKeyboardManager sharedManager].toolbarManageBehaviour = IQAutoToolbarByTag;
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES; // 控制是否显示键盘上的工具条

    //添加默认头部
    self.navigationController.navigationBarHidden = YES;
    _public_headView = [[PublicHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, TOPNAVIGATIONBAR_H)];
    [self.view addSubview:_public_headView];
    [_public_headView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.equalTo(@(TOPNAVIGATIONBAR_H));
    }];
    _public_headView.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    
    //    self.edgesForExtendedLayout = UIRectEdgeAll;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    //推送的通知。。。
//    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(pushNotifierAlertFunction:) name:PushNotifierName object:nil];
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用 iOS7 返回手势
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
}

  // 禁用 iOS7 返回手势实现方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return NO;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

}

- (UIButton*)leftButton
{
    UIImage *image = [UIImage imageNamed:@"back"];
    CGFloat buttonW = 44;
    CGFloat buttonH = 44;
    CGFloat jianGeW = 25;
    CGRect buttonFrame = CGRectMake(jianGeW , 20, buttonW, buttonH);
    
    UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
    [button addTarget:self action:@selector(leftButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:image forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    return button;
}
- (UIButton*)rightButton
{
    CGFloat buttonW = 44;
    CGFloat buttonH = 44;
    CGFloat jianGeW = 25;
    CGRect buttonFrame = CGRectMake(SCREEN_W-buttonW-jianGeW , 20, buttonW, buttonH);
    
    UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
    [button addTarget:self action:@selector(rightButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button sizeToFit];
    return button;
}
- (UILabel*)titleLabel
{
    CGRect labelFrame = CGRectMake(0, 0, 30, 44);
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    label.text = self.title;
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = [UIColor whiteColor];
    [label sizeToFit];
    
    return label;
}

-(void)leftButtonPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightButtonPressed
{
    
}
//添加刷新相关
-(void)addRefreshToTableView:(NSObject*)tableview
{
    //添加刷新
    if (_pullDownRefresh) {
        [_pullDownRefresh removeFromSuperview];
    }
    _pullDownRefresh = [[UIRefreshControl alloc] init];
    [_pullDownRefresh addTarget:self
                         action:@selector(refreshView:)
               forControlEvents:UIControlEventValueChanged];
    if ([tableview isKindOfClass:[UITableView class]]) {
         [(UITableView*)tableview addSubview:_pullDownRefresh];
    }else{
         [(UICollectionView*)tableview addSubview:_pullDownRefresh];
    }
   
}

-(void) refreshView:(UIRefreshControl *)refresh
{
    if (_rBlock) {
        _rBlock(refresh);
    }
}

refreshBlock _rBlock;
-(void)refreshAction:(refreshBlock)refreshblock
{
    _rBlock = refreshblock;
}

-(void)endRefresh
{
    [_pullDownRefresh endRefreshing];
}


//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
